package com.dmatek.uwb.util.tool;
/***
 * 解析工具
 * @author zhangfu
 * @data 2019年6月14日 下午6:32:08
 * @Description
 */
public final class AnalyticalUtil {
	public static final byte HEAD = (byte) 0xFC;
	// 播放音频应答包的包头
	public static final byte PLAYVOICE_RESPONSE_HEAD = (byte) 0xDE;
    public static final byte END = (byte) 0xFB;
    // 播放音频应答包的结尾
    public static final byte PLAYVOICE_RESPONSE_END = (byte) 0xDF;
    public static final byte TAG_TYPE = 0x01;
    public static final byte TAG_STILL_TYPE = 0x03;
    public static final byte ROUTER_TYPE = 0x02;
    public static final byte HEART_RATE_TYPE = 0x05;
    // 播放音频应答包的类型
    public static final byte PLAYVOICE_RESPONSE_TYPE = (byte)0x91;
    public static final byte TAG_COMMON_LOCAL = 0;
    public static final byte TAG_WARN_LOCAL = 1;
    public static final int TAG_STILL_SIZE = 13 + 6;
    public static final int TAG_PACK_NUM_PLACE = 11 + 6;
    public static final int TAG_PACK_SIZE = 19;
    public static final int ROUTER_PACK_SIZE = 13;
    public static final byte ROUTER_VERSION_SIZE = 6;
    // 心率數據包的長度
    public static final int HEART_RATE_PACK_SIZE = 8;
    // 播放音频回应的长度
    public static final int PLAYVOICE_RESPONSE_SIZE = 7;
    // 当检测到卡片与基站的距离小于卡片与基站的垂直距离时，此时应该采用的最短距离
    public final static int MIN_DISTANCE = 30;
    public final static int INITIAL_CAPACITY = 50;
    
	/**
	 * @param ch:查找的字符
	 * @param bytes:在bytes中查找
	 * @return:字符ch在字符串bytes中的位置
	 */
	public static int find(byte ch,byte[] bytes)
	{
		for(int i = 0;i < bytes.length; i ++)
		{
			if(ch == bytes[i])
			{
				return i;
			}
		}
		return -1;
	}
	
	public static int findLimitWithOrigin(int start, byte ch, byte[] bytes, int limit) {
		for(int i = start;i < limit; i ++)
		{
			if(ch == bytes[i])
			{
				return i;
			}
		}
		return -1;
	}
	
	public static String bytesToHex(byte[] bytes) 
	{
       StringBuilder buf = new StringBuilder(bytes.length * 2);
       for(byte b:bytes) {
           buf.append(String.format("%02X", new Integer(b & 0xff)));
       }
       return buf.toString();
   }
	
	public static byte[] hexStringToByteArray(String s) {
	    int len = s.length();
	    byte[] b = new byte[len / 2];
	    for (int i = 0; i < len; i += 2) {
	        // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个字节
	        b[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character
	                .digit(s.charAt(i + 1), 16));
	    }
	    return b;
	}
	public static String getVersion(int ver){
		byte year = (byte) (ver >> 24);
		byte month = (byte) (ver >> 16);
		byte day = (byte) (ver >> 8);
		byte v = (byte) (ver);
		return "V " + String.format("%02X", year)
				+ String.format("%02X", month)
				+ String.format("%02X", day) + Integer.toHexString(v & 0xFF);
	}
	/**
	 * 构建播放语音的数据包
	 * @return
	 */
	public static byte[] buildPlayVoiceBytes(String anchorId, String content, byte type, String encodeType) throws Exception {
		// 数据包总长度为248, 且创建时, 默认值都为0
		byte[] bytes = new byte[248];
		bytes[0] = (byte) 0xCE;
		bytes[1] = (byte) 0x91;
		// 基站类型：当前默认值为0x01
		bytes[2] = type;
		// 将AnchorId转成16进制的byte
		byte[] ids = hexStringToByteArray(anchorId);
		if (null == ids || ids.length != 2) { // id必须是两个字节
			throw new Exception("anchorId is not valid");
		}
		// 赋值基站Id
		System.arraycopy(ids, 0, bytes, 3, 2);
		// 获取当前内容的gbk编码
		byte[] unicode = content.getBytes(encodeType);
		// 内容gbk编码的长度
		int length = unicode.length;
		if (length > 240 || length <= 0) {
			throw new Exception("Voice packet content range is 0< size <= 240");
		}
		bytes[5] = (byte) (length & 0xFF);
		// 拷贝内容
		System.arraycopy(unicode, 0, bytes, 6, length);
		// 计算校验
		short cs = 0;
		for (int i = 0, size = bytes.length - 2; i < size; i++) {
			cs += bytes[i];
		}
		bytes[bytes.length - 2] = (byte) (cs & 0xFF);
		bytes[bytes.length - 1] = (byte) 0xCF;
		return bytes;
	}
}
